/**
 * 
 */
package com.newture.lxz.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;
import java.util.Map;

/**
 * @author xiao.hu
 *
 */
public class JSONKit {
	static final ObjectMapper mapper = new ObjectMapper();   

	public static List<Map<String, Object>> toListMap(String json) {
		try {
			return mapper.readValue(json, new TypeReference<List<Map<String, Object>>>(){});
		} catch (Exception e) {
			e.printStackTrace();
			throw new IllegalArgumentException(e);
		} 
	}
	
	
	public static <T> String toJSON(T obj) {
		try {
			return mapper.writeValueAsString(obj);
		} catch (Exception e) {
			e.printStackTrace();
			throw new IllegalArgumentException(e);
		}
	}
	
	public static <T> T parseObject(String jsonStr) {
		try {
			return mapper.readValue(jsonStr, new TypeReference<T>(){});
		} catch (Exception e) {
			e.printStackTrace();
			throw new IllegalArgumentException(e);
		} 
	}
}
